﻿//********************************************************************
// Author:      刘艳明
// Create Date: 2013-03-20
// Description: 
//
// Version:     1.0.0.0
// Change History:
// Date		    Who		      Changes Made
//--------------------------------------------------------------------
// Please Insert the Modiy record here!
//--------------------------------------------------------------------
//********************************************************************


using System;
using System.Collections.Generic;
using System.Data;
using MillerApp.CommonLib.DataAccess;
using MillerApp.DataContract.MillerDB;
using Npgsql;
using NpgsqlTypes;
using MillerApp.CommonLib.Utility;

namespace MillerApp.DataAccess.MillerDB
{
	/// <summary>
	/// 这个类提供了对Fundflow ()表的数据库操作
	/// </summary>
	public static partial class FundflowManager
	{
		 private static IDBHelper db = MillerAppDBHelper.CreateDatabase();

		static string insertSQL ="INSERT INTO fundflow (tradeday,outflowstockscount,inflowstockscount,seriesdays,inflowpfr,netflow,outflow,inflow,itemcode) VALUES (@tradeday,@outflowstockscount,@inflowstockscount,@seriesdays,@inflowpfr,@netflow,@outflow,@inflow,@itemcode)";
	
		static string updateSQL ="Update fundflow Set tradeday=@tradeday,outflowstockscount=@outflowstockscount,inflowstockscount=@inflowstockscount,seriesdays=@seriesdays,inflowpfr=@inflowpfr,netflow=@netflow,outflow=@outflow,inflow=@inflow,itemcode=@itemcode Where id=@id";

		static string deleteSQL ="DELETE FROM  fundflow  Where id=@id";	
		
		#region codesmith生成的代码
		/// <summary>
		/// 获取FundflowEntity实体 
		/// </summary>
		/// <param name="id">id</param>
		/// <returns>FundflowEntity</returns>
		public static FundflowEntity GetFundflowEntity(int id)
		{
			string strSQL = "select * from fundflow where id=@id";		
			IDbDataParameter para_id = new NpgsqlParameter("id", NpgsqlDbType.Integer);
			para_id.Value = id;
			IDbDataParameter[] paras=new IDbDataParameter[]{para_id};
			
			
			FundflowEntity entity=new FundflowEntity();
			using(System.Data.IDataReader reader=db.ExecuteReader(strSQL,paras))
			{
				if(reader.Read()==false) return null;
				LoadFromReader(reader, ref entity);
				return entity;
			}
		}		
		
		/// <summary>
		/// 更新FundflowEntity
		/// </summary>
		/// <param name="entity">Entity</param>	
		public static void UpdateFundflow(FundflowEntity entity)
		{
			if(entity==null)
			{
				throw new Exception("参数[entity]为[null]！");
			}
			IDbDataParameter[] paras =GenerateParas(entity);		
			db.ExecuteNonQuery(updateSQL,paras);
		}
		
		
				
        
        /// <summary>
		///  插入一条记录
		/// </summary>
		/// <param name="id">id</param>		
		public static void InsertFundflow(FundflowEntity entity)
		{
			if (entity == null)
            {
                throw new Exception("参数[entity]为[null]！");
            }
            IDbDataParameter[] paras = GenerateParas(entity);
            db.ExecuteNonQuery(insertSQL, paras);
		}
		
		/// <summary>
		///  删除一条记录
		/// </summary>
		/// <param name="id">id</param>		
		public static void DeleteFundflow(int id)
		{
			IDbDataParameter para_id = new NpgsqlParameter("id", NpgsqlDbType.Integer);
			para_id.Value = id;
			IDbDataParameter[] paras=new IDbDataParameter[]{para_id};
			db.ExecuteNonQuery(deleteSQL,paras);
		}

	
		
		/// <summary>
		/// 删除多条记录
		/// </summary>
		/// <param name="ids">ids</param>		
		public static void DeleteFundflow(List<int> ids)
		{
			string strSQL = "DELETE FROM  fundflow  Where id in ({0}) ";
            string deleteIds = StringHelper.ConvertListToSQLString<int>(ids);
            db.ExecuteNonQuery(string.Format(strSQL, deleteIds),null);
		}
		
		
	
		/// <summary>
		/// 根据分页条件获取Fundflow数据集
		/// </summary>
		/// <param name="conditions">条件</param>	
		/// <param name="sortExpression">排序表达式，为空则默认为:id</param>		
		/// <param name="pageIndex">开始页码：分页查询中的开始页码</param>
		/// <param name="pageSize">页宽：分页查询中的每页的记录数</param>
		/// <param name="recordCount">页宽：分页查询中的每页的记录数</param>		
		/// <returns>数据集(List)</returns>
		public static List<FundflowEntity> GetFundflowsByCond(string conditions,string sortExpression,int pageIndex,int pageSize,out int recordCount)
		{
			string viewName="fundflow";
			return db.GetDataListByCond<FundflowEntity>
                (viewName, conditions, sortExpression, pageIndex, pageSize, out recordCount, LoadFromReader);
		}
				
		
		/// <summary>
		/// 根据条件获取Fundflow数据集
		/// </summary>
		/// <param name="fields">查询的列(如果为空，则表示查询所有列)</param>	
		/// <param name="conditions">条件</param>	
		/// <param name="sortExpression">排序表达式，为空则默认为:id</param>		
		/// <returns>数据集(DataTable)：<br/></returns>
		public static DataTable GetFundflowsByCond(string fields,string conditions,string sortExpression)
		{		
			string viewName="fundflow";	
			return db.GetDataTableByCond(viewName, fields, conditions, sortExpression);		
		}
		#region private method
		/// <summary>
        /// 获取FundflowEntity实体数据更新参数
        /// </summary>
        /// <param name="entity">CompanyEntity</param>
        /// <returns></returns>
        private static IDbDataParameter[] GenerateParas(FundflowEntity entity)
        { 
			IDbDataParameter para_tradeday = new NpgsqlParameter("tradeday", NpgsqlDbType.Date);
			para_tradeday.Value = entity.tradeday;
			
			IDbDataParameter para_outflowstockscount = new NpgsqlParameter("outflowstockscount", NpgsqlDbType.Integer);
			para_outflowstockscount.Value = entity.outflowstockscount;
			
			IDbDataParameter para_inflowstockscount = new NpgsqlParameter("inflowstockscount", NpgsqlDbType.Integer);
			para_inflowstockscount.Value = entity.inflowstockscount;
			
			IDbDataParameter para_seriesdays = new NpgsqlParameter("seriesdays", NpgsqlDbType.Integer);
			para_seriesdays.Value = entity.seriesdays;
			
			IDbDataParameter para_inflowpfr = new NpgsqlParameter("inflowpfr", NpgsqlDbType.Numeric);
			para_inflowpfr.Value = entity.inflowpfr;
			
			IDbDataParameter para_netflow = new NpgsqlParameter("netflow", NpgsqlDbType.Numeric);
			para_netflow.Value = entity.netflow;
			
			IDbDataParameter para_outflow = new NpgsqlParameter("outflow", NpgsqlDbType.Numeric);
			para_outflow.Value = entity.outflow;
			
			IDbDataParameter para_inflow = new NpgsqlParameter("inflow", NpgsqlDbType.Numeric);
			para_inflow.Value = entity.inflow;
			
			IDbDataParameter para_itemcode = new NpgsqlParameter("itemcode", NpgsqlDbType.Varchar);
			para_itemcode.Value = entity.itemcode;
			
			IDbDataParameter para_id = new NpgsqlParameter("id", NpgsqlDbType.Integer);
			para_id.Value = entity.id;
			
			return new IDbDataParameter[]{para_tradeday,para_outflowstockscount,para_inflowstockscount,para_seriesdays,para_inflowpfr,para_netflow,para_outflow,para_inflow,para_itemcode,para_id};	
        }
		
		/// <summary>
		/// 绑定数据到业务实体
		/// </summary>
		/// <param name="reader">DataReader</param>
		/// <param name="entity">业务实体</param>
		private static void LoadFromReader(IDataReader reader,ref FundflowEntity entity)
		{			
			if (reader != null && !reader.IsClosed)
			{
				//entity.Id = reader.GetInt32(0);
                if(!Convert.IsDBNull(reader["tradeday"]))entity.tradeday = (DateTime)reader["tradeday"];
                if(!Convert.IsDBNull(reader["outflowstockscount"]))entity.outflowstockscount = (int)reader["outflowstockscount"];
                if(!Convert.IsDBNull(reader["inflowstockscount"]))entity.inflowstockscount = (int)reader["inflowstockscount"];
                if(!Convert.IsDBNull(reader["seriesdays"]))entity.seriesdays = (int)reader["seriesdays"];
                if(!Convert.IsDBNull(reader["inflowpfr"]))entity.inflowpfr = (decimal)reader["inflowpfr"];
                if(!Convert.IsDBNull(reader["netflow"]))entity.netflow = (decimal)reader["netflow"];
                if(!Convert.IsDBNull(reader["outflow"]))entity.outflow = (decimal)reader["outflow"];
                if(!Convert.IsDBNull(reader["inflow"]))entity.inflow = (decimal)reader["inflow"];
                if(!Convert.IsDBNull(reader["itemcode"]))entity.itemcode = reader["itemcode"].ToString();
                if(!Convert.IsDBNull(reader["id"]))entity.id = (int)reader["id"];
			}
			
		}
		#endregion	
		
		#endregion
		
		#region 手工增加的代码
        public static void DeleteFundFlowsByDateTime(DateTime startTime, DateTime endTime)
        {
            string delSQL = "DELETE FROM  fundflow  Where tradeday between @startdate and @enddate";
            IDbDataParameter para_1 = new NpgsqlParameter("startdate", NpgsqlDbType.Date);
            para_1.Value = startTime;
            IDbDataParameter para_2 = new NpgsqlParameter("enddate", NpgsqlDbType.Date);
            para_2.Value = endTime;
            IDbDataParameter[] paras = new IDbDataParameter[] { para_1,para_2 };
            db.ExecuteNonQuery(delSQL, paras);
        }
        #endregion
	}
}
